<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\common\logic\home;

use app\common\model\RecordClassModel;
use app\common\model\RecordModel;
use app\common\model\RelationStudentModel;

class Relation
{
	/**
	 * [或者发送记录中 课题ID和用户的关心数组集合]
	 * @param  [type] $taskid  [课题ID]
	 * @param  [type] $user_id [用户ID]
	 * @return [type]          [description]
	 */
	public function getGrouplist($taskid,$user_id){
		//获取发送课题记录信息
        $data_record = RecordModel::where('mytask_id',$taskid)->where('s_user_id',$user_id)->field('class_id,status,record_class_id')->find();
        if(empty($data_record)){
        	return 0;
        }
        //如果是小组
        if($data_record['status'] == 3){
        	$data = RecordModel::where('record_class_id',$data_record['record_class_id'])->where('status',3)->field('mytask_id,s_user_id')->select();
        	$mytask_ids = [];
        	foreach ($data as $key => $value) {
        		$mytask_ids[$value['s_user_id']] = $value['mytask_id'];
        	}
        }else{
        	$mytask_ids[$user_id] = $taskid;
        }

        return $mytask_ids;
	}
	/**
	 * [判断课题拥有者身份]
	 * @param  [type] $taskid  [课题ID]
	 * @param  [type] $user_id [用户ID]
	 * @return [type]          [description]
	 */
	public function checkGroup($taskid,$user_id){
		
		$data = RecordModel::where('mytask_id',$taskid)->where('s_user_id',$user_id)->field('class_id')->find();

		if(!empty($data)){
			$level = RelationStudentModel::where('user_id',$user_id)->where('class_id',$data['class_id'])->value('level');
		
			if($level == 1){
				return ['code'=>1,'msg'=>'组长'];
				
			}else{
				return ['code'=>0,'msg'=>'组员'];
			}
		}else{
			return ['code'=>-1,'msg'=>'非组员'];
		}
	}
	
}